﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IAC_2013.com.iac.util;
using IAC_2013.com.iac.form;
using IAC_2013.com.iac.action;
using System.Data.SqlClient;

namespace IAC_2013.Forms
{
    public partial class frmUser : Form
    {
        PositionsForm positionsForm;
        UserForm userForm;
        DataTable tb;
        AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
        public frmUser()
        {
            InitializeComponent();
            load_positions();
        }

        private void load_positions()
        {
            tb = new UserAction().listPositions();
            txtPositionsId.DataSource = tb;
            txtPositionsId.DisplayMember = "ten_cv";
            txtPositionsId.ValueMember = "id_cv";
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            var text = txtPositionsId.SelectedText;
            MessageBox.Show(text);
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            positionsForm = new PositionsForm();
            userForm = new UserForm();
            String positionsName = txtPositionsId.Text;
            positionsForm.name = positionsName;
            int positonsId = new UserAction().addPositions(positionsForm);
            userForm.positionsId = positonsId;

            userForm.code = txtCode.Text;
            userForm.name = txtName.Text;
            userForm.userName = txtUserName.Text;
            userForm.password = txtPassword.Text;
            userForm.address = txtAddress.Text;
            userForm.phone = txtPhone.Text;
            userForm.email = txtEmail.Text;

            String code = txtCode.Text;
            Boolean checkCode = CommonUtils.checkCode("", code, "ma_nd", "nd_nguoi_dung", "add");

            if (checkCode == false)
            {
                MessageBox.Show("Mã nhân viên đã tồn tại.");
            }
            else
            {

            }
        }

        private void txtPhone_TextChanged(object sender, EventArgs e)
        {
            String word = txtPhone.Text;
            txtPhone.Text = ValidateUtils.checkNum(word);
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {
            namesCollection = CommonUtils.autoCompleted("nd_nguoi_dung", "ten_dang_nhap");
            txtUserName.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtUserName.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtUserName.AutoCompleteCustomSource = namesCollection;
        }

        private void txtCode_TextChanged(object sender, EventArgs e)
        {
            namesCollection = CommonUtils.autoCompleted("nd_nguoi_dung", "ma_nd");
            txtCode.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtCode.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtCode.AutoCompleteCustomSource = namesCollection;
        }

        private void txtPositionsId_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}
